package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.Channel;
import cn.nubia.admin.application.bean.Claim;
import cn.nubia.admin.application.bean.ClaimVO;
import cn.nubia.admin.application.query.ClaimQuery;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.DictField;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.DictFieldItemService;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by justin on 15/7/12.
 */
@Service
public class ClaimService extends BaseServiceImpl<Claim>{
    @Autowired
    DictFieldItemService dictFieldItemService;
    @Autowired
    SystemLogService systemLogService;

    public Paged<ClaimVO> listClaims(ClaimQuery claimQuery) {
        Query query = Query.build(Claim.class);
        query.addNotEq(Claim.COLUMN_STATE, Claim.STATE_DEL);
        query.addOrder(Claim.COLUMN_CREATETIME, Query.DBOrder.DESC);
        if(claimQuery.getClaimType()!=null && claimQuery.getClaimType() >=0){
            query.addEq(Claim.COLUMN_CLAIMTYPE, claimQuery.getClaimType());
        }
        if(claimQuery.getResType()!=null && claimQuery.getResType() >=0){
            query.addEq(Claim.COLUMN_RESTYPE,claimQuery.getResType());
        }
        if(claimQuery.getUserName()!=null && !"".equals(claimQuery.getUserName())){
            query.addLike(Claim.COLUMN_USERNAME, claimQuery.getUserName());
        }
        if(claimQuery.getCreateTimes()!=null && claimQuery.getCreateTimee() != null){
            query.addBetween(Claim.COLUMN_CREATETIME, DateUtil.getIntegerTime(claimQuery.getCreateTimes()), DateUtil.getIntegerTime(claimQuery.getCreateTimee()));
        }
        query.setPaged(claimQuery.getPageNo(), claimQuery.getPageSize());
        Paged<Claim> claimPaged = findPagedByQuery(query);
        Paged<ClaimVO> claimVOPaged = new Paged<>();
        List<ClaimVO> claimVOs = new ArrayList<>();
        for(Claim claim : claimPaged.getListData()){
            ClaimVO vo = new ClaimVO();
            BeanUtils.copyProperties(claim, vo);
            vo.setClaimTypeName(dictFieldItemService.getItemKeyByValue(DictField.DICTFIELDITEM_CLAIM, vo.getClaimType()));
            vo.setCreateTimeShow(DateUtil.getFormatDate(vo.getCreateTime()));
            vo.setResTypeName(Claim.resTypeMap.get(String.valueOf(vo.getResType())));
            vo.setStateShow(Claim.stateMap.get(String.valueOf(vo.getState())));
            claimVOs.add(vo);
        }
        claimVOPaged.setPageNo(claimPaged.getPageNo());
        claimVOPaged.setPageSize(claimPaged.getPageSize());
        claimVOPaged.setListData(claimVOs);
        claimVOPaged.setTotalCount(claimPaged.getTotalCount());
        return claimVOPaged;
    }

    public ClaimVO getVOById(Integer id) {
        ClaimVO vo = new ClaimVO();
        Claim claim = get(id);
        BeanUtils.copyProperties(claim,vo);
        vo.setClaimTypeName(dictFieldItemService.getItemKeyByValue(DictField.DICTFIELDITEM_CLAIM, vo.getClaimType()));
        vo.setCreateTimeShow(DateUtil.getFormatDate(vo.getCreateTime()));
        vo.setResTypeName(Claim.resTypeMap.get(String.valueOf(vo.getResType())));
        return vo;
    }

    public void changeState(Integer id,Integer state){
        Claim claim = get(id);
        claim.setState(state);
        update(claim);
        //log
        if(state == Claim.STATE_DEL){
            systemLogService.log(SystemLog.ModuleTypeEnum.CLAIMLIST, SystemLog.OperateTypeEnum.DEL, id);
        }else if(state == Claim.STATE_HIDDEN){
            systemLogService.log(SystemLog.ModuleTypeEnum.CLAIMLIST, SystemLog.OperateTypeEnum.HIDDEN, id);
        }else if(state == Claim.STATE_READED){
            systemLogService.log(SystemLog.ModuleTypeEnum.CLAIMLIST, SystemLog.OperateTypeEnum.READED, id);
        }
    }
}
